/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netbeans.shapesimple;


import java.awt.BorderLayout;
import java.util.Collection;
import javax.swing.JScrollPane;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.shapesimple.palette.PaletteSupport;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;


/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.netbeans.shapesimple//Shape//EN" ,
        autostore = false )
@TopComponent.Description(
        preferredID = "ShapeTopComponent" ,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( position = 7, mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "org.netbeans.shapesimple.ShapeTopComponent" )
@ActionReferences({
    @ActionReference( path = "Menu/Window" , position = 1033  ),
    @ActionReference( path = "Menu/File/Open" , position = 1900  )
})
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_ShapeAction" ,
        preferredID = "ShapeTopComponent" )
@Messages(
        {
    "CTL_ShapeAction=Shape" ,
    "CTL_ShapeTopComponent=Shape Window" ,
    "HINT_ShapeTopComponent=This is a Shape window"
} )
public final class ShapeTopComponent extends TopComponent implements ExplorerManager.Provider, LookupListener
{

    private transient ExplorerManager exploMan = new ExplorerManager();
    
    private Lookup.Result<Node> nodeResultListener;
    
    public ShapeTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_ShapeTopComponent() );
        setToolTipText( Bundle.HINT_ShapeTopComponent() );
        
        setLayout(new BorderLayout());
        GraphSceneImpl scene = new GraphSceneImpl();
        
        JScrollPane shapePane = new JScrollPane();
        
        shapePane.setViewportView( scene.createView());
        add(shapePane, BorderLayout.CENTER);
        add(scene.createSatelliteView(), BorderLayout.WEST);

        exploMan = new ExplorerManager();
        
        ProxyLookup pl = new ProxyLookup(new Lookup[]{
            ExplorerUtils.createLookup( exploMan , getActionMap() ),
            Lookups.fixed( PaletteSupport.createPalette() )

        });
        associateLookup(pl);
        //associateLookup(ExplorerUtils.createLookup( exploMan , getActionMap() ));
        
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened()
    {
        this.nodeResultListener = Utilities.actionsGlobalContext().lookupResult(Node.class);
        this.nodeResultListener.addLookupListener( this);
    }


    @Override
    public void componentClosed()
    {
        this.nodeResultListener.removeLookupListener( this );
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    @Override
    public ExplorerManager getExplorerManager()
    {
        return exploMan;
    }


    @Override
    public void resultChanged( LookupEvent le )
    {
        Collection<? extends Node> allNodes = nodeResultListener.allInstances();
        
        for (Node n : allNodes )
        {
            System.out.println( "ShapeTopComponent : " + n);
        }
    }

}
